# =============================================================================
# APPENDIX N FIGURE 18A: CORRELATIONS WITH RELATED MEASURES
# Scatter plot matrix comparing latent measure with existing measures
# =============================================================================

# --- PACKAGES ----------------------------------------------------------------
library(dplyr)
library(ggplot2)
library(GGally)
library(countrycode)

# --- DATA LOADING ------------------------------------------------------------
dat <- read.csv("estimates_independent.csv")
gandhi <- read.csv("gandhi-sumner-estimates.csv")
ecap <- read.csv("eca data.csv")
personalism <- read.csv("GWF+personalism-scores.csv")

# --- DATA PREPARATION --------------------------------------------------------
# Convert country names to COW codes where needed
gandhi$COWcode <- countrycode(gandhi$country, origin = "country.name", destination = "cown")
ecap$COWcode <- countrycode(ecap$country, origin = "country.name", destination = "cown")
personalism <- personalism %>% rename(COWcode = cowcode)

# Merge datasets on COWcode and year
merged_data <- dat %>%
  inner_join(gandhi, by = c("COWcode", "year")) %>%
  inner_join(ecap, by = c("COWcode", "year")) %>%
  inner_join(personalism, by = c("COWcode", "year")) %>%
  rename(country = country.y)  # Standardize country column name

# Create analysis subset with renamed variables
df_subset_unnorm <- merged_data %>%
  select(
    country, year,
    proposed_measure = dyn.estimates,
    GWF_measure = latent_personalism,
    Gandhi_Sumner_measure = xhatmedian,
    Kaire_measure = eca
  )

# --- VISUALIZATION -----------------------------------------------------------
plot_matrix <- ggpairs(
  df_subset_unnorm %>% select(-country, -year),
  columnLabels = c("Latent Measure", "Geddes et al.", "Gandhi and Sumner", "Kaire"),
  lower = list(continuous = wrap("points", alpha = 0.6)),
  diag = list(continuous = wrap("densityDiag", alpha = 0.6)),
  upper = list(continuous = wrap("cor", size = 4))
)

print(plot_matrix)

# --- OUTPUT ------------------------------------------------------------------
ggsave("Figure18A.pdf", plot = plot_matrix, width = 10, height = 8)